package com.barbeque.mall.controller.wx;

import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.user.MarketAddress;
import com.barbeque.mall.service.wx.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 苏东宇
 * @since 2022/07/19 16:14
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {


    @Autowired
    WxAddressService wxAddressService;

    @RequestMapping("list")
    public BaseRespVo getAddressList(){
        CommonData addressList = wxAddressService.getAddressList();
        return BaseRespVo.ok(addressList);
    }

   @RequestMapping("save")
    public BaseRespVo createAddress(@RequestBody MarketAddress marketAddress){
       //根据id是否为0判断是创建新的地址还是修改地址信息
       Integer id = marketAddress.getId();
       //判断是否设置了默认地址选项，默认地址只允许有一个，如果有了的话，修改所有设置了true的数据
       if(marketAddress.getIsDefault()){
           int i = wxAddressService.setDefault();
       }
       if (id == 0){
           //此时为新建地址信息
           int address = wxAddressService.createAddress(marketAddress);
           if (address == 1){
               return BaseRespVo.ok(marketAddress);
           }
       }else {
           //此时为修改地址信息
           int i = wxAddressService.updateAddress(marketAddress);
           if (i == 1){
               return BaseRespVo.ok(marketAddress);
           }
       }


       return BaseRespVo.invalidData("创建失败");
   }

   @RequestMapping("detail")
    public BaseRespVo detailAddress(Integer id){
       MarketAddress marketAddress = wxAddressService.detailAddress(id);
       return BaseRespVo.ok(marketAddress);
   }

   @RequestMapping("delete")
    public BaseRespVo deleteAddress(@RequestBody MarketAddress marketAddress){
       int i = wxAddressService.deleteAddress(marketAddress.getId());
       if (i == 1){
          return BaseRespVo.ok(null);
       }
       return BaseRespVo.invalidData("删除失败");
   }
}
